package com.example.task1.ui.AllService.logistics;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.LogisticsComplaintBean;
import com.example.task1.http.Api;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import retrofit2.Call;

public class LogisticsComplaintActivity extends AppCompatActivity {

    private RecyclerView rvList;
    private int pageNum;
    private LinearLayoutManager linearLayoutManager;
    private LogisticsComplaintAdapter logisticsComplaintAdapter;
    private boolean isTrue = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logistics_complaint);
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setTitle("投诉记录");
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        initView();
        initData();
        setAdapter();
        loadCommentList();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    private void initData() {
        pageNum = 1;
        Call<ApiResp> apiRespCall = Api.api().get(MyApplication.TOKEN, "/prod-api/api/logistics-inquiry/logistics_complaint/my-list?pageNum=" + pageNum + "&pageSize=10");
        Api.send(apiRespCall, LogisticsComplaintBean.class, new CallBack<LogisticsComplaintBean>() {
            @Override
            public void success(LogisticsComplaintBean data) {
                if (logisticsComplaintAdapter.getData().size() < data.getTotal()) {
                    pageNum++;
                    isTrue = true;
                    logisticsComplaintAdapter.setData(data.getRows());
                } else {
                    MyApplication.show("没有更多数据了");
                }
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    private void loadCommentList() {
        rvList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                if (logisticsComplaintAdapter.getItemCount() - 1 == linearLayoutManager.findLastVisibleItemPosition() && isTrue) {
                    isTrue = false;
                    initData();
                }
            }
        });
    }

    private void setAdapter() {
        logisticsComplaintAdapter = new LogisticsComplaintAdapter(LogisticsComplaintActivity.this);
        linearLayoutManager = new LinearLayoutManager(LogisticsComplaintActivity.this);
        rvList.setAdapter(logisticsComplaintAdapter);
        rvList.setLayoutManager(linearLayoutManager);
    }

    private void initView() {
        rvList = (RecyclerView) findViewById(R.id.rv_list);
    }
}